﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using zichan.DAL;
using zichan.Entity;
using zichan.BLL.StatisticsModel;

namespace zichan.BLL
{
    public class StatisticsService
    {
        public static List<AssetStatiticsModel> AssetStatisticsList(int? assetClassId, int? serverPurposeId, int? companyId, DateTime? sdt, DateTime? edt)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                IQueryable<Asset> asset = db.Assets.Where(o => o.Status > 0);
                if (assetClassId.HasValue)
                    asset = asset.Where(o => o.AssetClassId == assetClassId);
                if(serverPurposeId.HasValue)
                {
                    List<string> codes = db.PutawayServers.Where(o => o.serverPurposeId == serverPurposeId).Select(o => o.Code).ToList();
                    asset = asset.Where(o => codes.Contains(o.Code));
                }
                if (companyId.HasValue)
                    asset = asset.Where(o => o.CompanyId == companyId);
                if (sdt.HasValue)
                    asset = asset.Where(o => o.BuyTime >= sdt);
                if (edt.HasValue)
                {
                    edt = DateTime.Parse(string.Format("{0:yyyy-MM-dd 23:59:59}",edt));
                    asset = asset.Where(o => o.BuyTime <= edt);
                }
                List<int> ids = asset.Select(o => o.AssetClassId).Distinct().ToList();
                List<AssetStatiticsModel> list = new List<AssetStatiticsModel>();
                foreach (int id in ids)
                {
                    AssetStatiticsModel model = new AssetStatiticsModel();
                    model.Id = id;
                    model.Name = AsseteClassService.GetAsseteClass(id).Name;
                    IQueryable<Asset> assetList = asset.Where(o => o.AssetClassId == id);
                    model.RepertoryNumber = assetList.Count(o => o.Status == 1);
                    model.InuseNumber = assetList.Count(o => o.Status == 2);
                    model.ScrapNumber = assetList.Count(o => o.Status == 3);
                    model.TotalNumber = assetList.Count();
                    list.Add(model);
                }
                return list;
            }
        }
        public static List<IpStatisticsModel> IpStatisticsList(int? machineRoomId)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                List<IpStatisticsModel> list = new List<IpStatisticsModel>();
                List<int> mIds = new List<int>();
                if (machineRoomId.HasValue)
                    mIds.Add(machineRoomId.Value);
                else
                    mIds = db.MachineRooms.Where(o=>!o.IsDelete.HasValue).Select(o => o.Id).ToList();
                foreach (int id in mIds)
                {
                    IpStatisticsModel model = new IpStatisticsModel();
                    IQueryable<PutawayServer> p = db.PutawayServers.Where(o => o.MachineRoomId == id && o.Statu == 1);
                    List<int> ids = p.Select(o => o.IPId.Value).ToList();
                    List<string> arrIds = p.Where(o => !string.IsNullOrEmpty(o.SubIPIds)).Select(o => o.SubIPIds).ToList();
                    string subIpIds = String.Join(",", arrIds);
                    //ids.AddRange(subIpIds);
                    model.MachineRoomName = MachineRoomService.Get(id).Name;
                    model.UsedIp = ids.Count() + subIpIds.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Count();
                    model.MachineRoomIp = db.IPs.Where(o => o.MachineRoomId == id&&!o.isDelete.HasValue).Count();
                    model.SubIp = model.MachineRoomIp - model.UsedIp;
                    model.MachineRoomId = id;
                    list.Add(model);
                }
                return list;
            }
        }
        public static List<CabinetStatisticsModel> CabinetStatisticsList(int? machineRoomId)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                List<int> mIds = new List<int>();
                if (machineRoomId.HasValue)
                    mIds.Add(machineRoomId.Value);
                else
                    mIds = db.Cabinets.Select(o => o.MachineRoomId).Distinct().ToList();
                List<CabinetStatisticsModel> list = new List<CabinetStatisticsModel>();
                foreach (int id in mIds)
                {
                    List<int> cIds = db.Cabinets.Where(o => o.MachineRoomId == id&&!o.IsDelete.HasValue).Select(o => o.Id).Distinct().ToList();
                    foreach (int cid in cIds)
                    {
                        Cabinet c = CabinetManageService.GetCabinet(cid);
                        CabinetStatisticsModel model = new CabinetStatisticsModel();
                        model.MachineRoomName = MachineRoomService.Get(id).Name;
                        model.CabinetId = cid;
                        model.TotalNumber = c.Number;
                        model.CabinetName = c.Name;
                        if (db.PutawayServers.Count(o => o.CabinetId == cid && o.UseNumber.HasValue && o.Statu == 1) > 0)
                            model.UsedNumber = db.PutawayServers.Where(o => o.CabinetId == cid && o.UseNumber.HasValue&&o.Statu==1).Sum(o => o.UseNumber.Value);
                        else
                            model.UsedNumber = 0;
                        model.SubNumber = model.TotalNumber - model.UsedNumber;
                        list.Add(model);
                    }
                }
                return list;
            }
        }
    }
}
